package com.prs.crm.service.cdxh.impl;

import java.util.List;

import com.prs.crm.dao.cdxh.NewPlanOrderDao;
import com.prs.crm.domain.cdxh.DistributionOrder;
import com.prs.crm.domain.cdxh.DistributionOrderDetail;
import com.prs.crm.domain.cdxh.MaterialProduct;
import com.prs.crm.domain.cdxh.NewPlanOrder;
import com.prs.crm.domain.cdxh.NewPlanOrderDetail;
import com.prs.crm.domain.cdxh.SerialNumberss;
import com.prs.crm.domain.cdxh.material.MaterialTeam;
import com.prs.crm.domain.cdxh.molding.Molding;
import com.prs.crm.domain.cdxh.molding.MoldingTeam;
import com.prs.crm.domain.cdxh.printing.Printing;
import com.prs.crm.domain.cdxh.printing.PrintingTeam;
import com.prs.crm.service.cdxh.DistributionOrderService;
import com.prs.crm.service.cdxh.MaterialProductService;
import com.prs.crm.service.cdxh.MaterialTeamService;
import com.prs.crm.service.cdxh.MoldingService;
import com.prs.crm.service.cdxh.MoldingTeamService;
import com.prs.crm.service.cdxh.NewPlanOrderService;
import com.prs.crm.service.cdxh.PrintingService;
import com.prs.crm.service.cdxh.PrintingTeamService;
import com.prs.crm.service.cdxh.SerialNumberssService;
import com.prs.crm.service.impl.BaseServiceImpl;

public class NewPlanOrderServiceImpl extends
		BaseServiceImpl<NewPlanOrder, NewPlanOrderDao> implements
		NewPlanOrderService {
	private MaterialProductService materialProductService;
	private SerialNumberssService serialNumberssService;
	private DistributionOrder distributionOrder;
	private DistributionOrderService distributionOrderService;

	private MaterialTeamService materialTeamService;

	private MoldingService moldingService;
	private MoldingTeamService moldingTeamService;

	private PrintingService printingService;
	private PrintingTeamService printingTeamService;

	@Override
	public void saveNewPlan(NewPlanOrder newPlanOrder, String[] productCodes,
			String[] pronames, String[] productNorms, String[] productColors,
			String[] productpackges, String[] productNums,
			String[] productUnits, String[] poductPrices,
			String[] prototalPrices) {
        String CodeNumbers="03040020101202H1";
		SerialNumberss snObject=this.getSerialNumberssService().getSerialNumberss("新建计划");
		if(snObject==null){
			SerialNumberss  snmm=new SerialNumberss();
			snmm.setStyCategory("新建计划");
			snmm.setStyNumberCode(CodeNumbers);
			this.getSerialNumberssService().save(snmm);
		}else{
			String codeF=snObject.getStyNumberCode();
			String DF[]=codeF.split("H");
			int lastCode=Integer.parseInt(DF[1])+1;
			CodeNumbers=DF[0]+"H"+lastCode;
			snObject.setStyNumberCode(CodeNumbers);
			this.getSerialNumberssService().setNumCode(snObject);
		}
		
		
		int lengths = productCodes.length;
		double totalPrice = 0.0;
		for (int i = 0; i < lengths; i++) {
		NewPlanOrderDetail npod = new NewPlanOrderDetail();
			npod.setProductCode(productCodes[i]);
			npod.setProductName(pronames[i]);
			npod.setProductNorm(productNorms[i]);
			npod.setProductColor(productColors[i]);
			npod.setProductPaceStyle(productpackges[i]);
			npod.setProductNum(Integer.parseInt(productNums[i]));
			npod.setPriceUnit(Double.parseDouble(poductPrices[i]));
			npod.setProductUnit(productUnits[i]);
			npod.setSubtotal(Double.parseDouble(prototalPrices[i]));
			totalPrice = totalPrice + Double.parseDouble(prototalPrices[i]);
			newPlanOrder.addDetail(npod);
			
		}
		newPlanOrder.setRushOrder("否");
		newPlanOrder.setNewProCode(CodeNumbers);
		newPlanOrder.setAllPrice(totalPrice);
		super.save(newPlanOrder);

		
		/**
		 * 加入配料单据里面
		 */

	for (int i = 0; i < lengths; i++) {
		
		MaterialProduct mpObject = new MaterialProduct();
		mpObject.setProductCode(productCodes[i]);
		mpObject.setProductName(pronames[i]);
		mpObject.setProductColor(productColors[i]);
		mpObject.setProductNorm(productNorms[i]);
		mpObject.setDeliveryDate(newPlanOrder.getSuppliesTime());
		mpObject.setRushOrder(newPlanOrder.getRushOrder());
		mpObject.setProductNum(Integer.parseInt(productNums[i]));
		mpObject.setPlanOrOrderId(CodeNumbers);
		mpObject.setProductPackstyle(productpackges[i]);
		mpObject.setRushOrder("否");
		mpObject.setBackMakeState(1);
		mpObject.setpLState(1);
		mpObject.setAlreadyProduct(0.000);
		mpObject.setFirstYN(1);
		mpObject.setRemainderNum(0.000);
		mpObject.setProductPackstyle(productpackges[i]);
		mpObject.setBackMakeState(1);
		mpObject.setNeedNum(0.000);
		mpObject.setNumConfirmState(1);
		this.getMaterialProductService().save(mpObject);
	}
	/**
	 * 添加到配送单里面
	 */
	DistributionOrder disObject=new DistributionOrder();
	for(int j=0;j<lengths;j++){
		DistributionOrderDetail  dbObject=new DistributionOrderDetail();
		dbObject.setProductCode(productCodes[j]);
		dbObject.setProductName(pronames[j]);
		dbObject.setProductNorm(productNorms[j]);
		dbObject.setProductColor(productColors[j]);
		dbObject.setProductPackWay(productpackges[j]);
		dbObject.setDisNum(Integer.parseInt(productNums[j]));
		dbObject.setProToNums(0);
		//平方
//		System.out.println("productNorms[j]="+productNorms[j]);
		String  pfs[]=productNorms[j].split("\\*");
		double pf=Double.parseDouble(pfs[1])*Double.parseDouble(productNums[j]);
		dbObject.setDisSquare(pf);
		dbObject.setDisPrice(Double.parseDouble(poductPrices[j]));
		dbObject.setDisTotal(Double.parseDouble(prototalPrices[j]));
		dbObject.setSaleOrderPlan(CodeNumbers);
		disObject.addDetail(dbObject);
		
	}
	disObject.setDisTotalMoney(0.0);
	disObject.setDisOrderCode(CodeNumbers);
	disObject.setDisOrderCustomer("新建计划单");
	disObject.setState("7");
   this.getDistributionOrderService().save(disObject);
	
	}

	public MaterialProductService getMaterialProductService() {
		return materialProductService;
	}

	public void setMaterialProductService(
			MaterialProductService materialProductService) {
		this.materialProductService = materialProductService;
	}

	public SerialNumberssService getSerialNumberssService() {
		return serialNumberssService;
	}

	public void setSerialNumberssService(
			SerialNumberssService serialNumberssService) {
		this.serialNumberssService = serialNumberssService;
	}

	public DistributionOrder getDistributionOrder() {
		return distributionOrder;
	}

	public void setDistributionOrder(DistributionOrder distributionOrder) {
		this.distributionOrder = distributionOrder;
	}

	public DistributionOrderService getDistributionOrderService() {
		return distributionOrderService;
	}

	public void setDistributionOrderService(
			DistributionOrderService distributionOrderService) {
		this.distributionOrderService = distributionOrderService;
	}

	@Override
	public void saveOrNewPlanOrder(NewPlanOrder newPlanOrder) {
		this.getDao().save(newPlanOrder);
		
	}

	@Override
	public void setAlllRushOrders(NewPlanOrder newPlanOrder) {
		String orderplan = newPlanOrder.getNewProCode();
		String runshs = newPlanOrder.getRushOrder();
//		System.out.println("急单=" + runshs);
//		System.out.println("orderplan=" + orderplan);
		/**
		 * 配料
		 */
		List<MaterialProduct> objectA = this.getMaterialProductService()
				.getMaterialProductOrderpLan(orderplan);
		int alengths = objectA.size();
		if (alengths != 0) {
			for (MaterialProduct aa : objectA) {
				aa.setRushOrder(runshs);
				this.getMaterialProductService().saveOrMaterialProduct(aa);
			}
		}

		List<MaterialTeam> objectB = this.getMaterialTeamService()
				.getMaterialTeamOrderpLan(orderplan);
		int blengths = objectB.size();
		if (blengths != 0) {
			for (MaterialTeam bb : objectB) {
				bb.setMaterialRushOrder(runshs);
				this.getMaterialTeamService().saveOrMaterialTeam(bb);
			}
		}
		/**
		 * 成型
		 */
		List<Molding> objectC = this.getMoldingService().getMoldingOrderpLan(
				orderplan);
		int clengths = objectC.size();
		if (clengths != 0) {
			for (Molding cc : objectC) {
				cc.setMoldingRushOrder(runshs);
				this.getMoldingService().saveOrMolding(cc);
			}
		}
		List<MoldingTeam> objectD = this.getMoldingTeamService()
				.getMoldingTeamOrderpLan(orderplan);
		int dlengths = objectD.size();
		if (dlengths != 0) {
			for (MoldingTeam dd : objectD) {
				dd.setRunshOrder(runshs);
				this.getMoldingTeamService().saveOrMoldingTeam(dd);
			}
		}
		/**
		 * 印刷
		 */
		List<Printing> objectE = this.getPrintingService()
				.getPrintingOrderpLan(orderplan);
		int Elengths = objectE.size();
		if (Elengths != 0) {
			for (Printing ee : objectE) {
				ee.setPrintRushOrder(runshs);
				this.getPrintingService().saveOrPrinting(ee);
			}
		}
		List<PrintingTeam> objectF = this.getPrintingTeamService()
				.getPrintingTeamOrderpLan(orderplan);
		int flengths = objectF.size();
		if (flengths != 0) {
			for (PrintingTeam ff : objectF) {
				ff.setPrintRushOrser(runshs);
				this.getPrintingTeamService().saveOrPrintingTeam(ff);
			}
		}
		
	}

	public MaterialTeamService getMaterialTeamService() {
		return materialTeamService;
	}

	public void setMaterialTeamService(MaterialTeamService materialTeamService) {
		this.materialTeamService = materialTeamService;
	}

	public MoldingService getMoldingService() {
		return moldingService;
	}

	public void setMoldingService(MoldingService moldingService) {
		this.moldingService = moldingService;
	}

	public MoldingTeamService getMoldingTeamService() {
		return moldingTeamService;
	}

	public void setMoldingTeamService(MoldingTeamService moldingTeamService) {
		this.moldingTeamService = moldingTeamService;
	}

	public PrintingService getPrintingService() {
		return printingService;
	}

	public void setPrintingService(PrintingService printingService) {
		this.printingService = printingService;
	}

	public PrintingTeamService getPrintingTeamService() {
		return printingTeamService;
	}

	public void setPrintingTeamService(PrintingTeamService printingTeamService) {
		this.printingTeamService = printingTeamService;
	}

}
